﻿using System;
using System.Data;

namespace Utils.Data 
{
/// <summary>
/// Summary description for DataSetGastArbeiter
/// Used to perform various operations on DataSets
/// </summary>
    public class DataSetGastArbeiter
        {
        public DataSetGastArbeiter ( )
            {

            }

        #region Methods
        public static void CheckForUnfilledColumns ( ref string msg, ref DataSet ds )
            {
            if (ds == null)
                {
                msg = "null dataset passed";
                }
            else //the ds passed is not null 
                {

                    if (ds.Tables.Count == 0)
                    {
                        DataTable dt = new DataTable();
                        

                        DataColumn colColumnName = new DataColumn();
                        DataColumn colDataType = new DataColumn();
                        DataColumn colMaxLength = new DataColumn();
                        DataColumn colFilledValue = new DataColumn();
                        

                        colColumnName.ColumnName = "COLUMN_NAME";
                        colColumnName.DataType = Type.GetType("System.String");

                        colDataType.ColumnName = "DATA_TYPE";
                        colDataType.DataType = Type.GetType("System.String");


                        colMaxLength.ColumnName = "MAX_LENGTH";
                        colMaxLength.DataType = Type.GetType("System.String");

                        colFilledValue.ColumnName = "filledValue";
                        colFilledValue.DataType = Type.GetType("System.String");

                        // Add these DataColumns into the DataTable
                        dt.Columns.Add(colColumnName);
                        dt.Columns.Add(colDataType);
                        dt.Columns.Add(colMaxLength);
                        dt.Columns.Add(colFilledValue);

                        ds.Tables.Add(dt);

                    } //of 


                    if (ds.Tables[0].Columns["COLUMN_NAME"] == null)
                    { ds.Tables[0].Columns.Add("COLUMN_NAME", typeof(string)); }


                    if (ds.Tables[0].Columns["DATA_TYPE"] == null)
                    { ds.Tables[0].Columns.Add("DATA_TYPE", typeof(string)); }

                    if (ds.Tables[0].Columns["MAX_LENGTH"] == null)
                    { ds.Tables[0].Columns.Add("MAX_LENGTH", typeof(int)); }

 
                    if (ds.Tables [ 0 ].Columns [ "include" ] == null)
                    { ds.Tables [ 0 ].Columns.Add ( "include", typeof ( bool ) ); }

                    if (ds.Tables [ 0 ].Columns [ "filledValue" ] == null)
                    { ds.Tables [ 0 ].Columns.Add ( "filledValue", typeof ( string ) ); }

                    if (ds.Tables [ 0 ].Columns [ "DESCRIPTION" ] == null)
                    { ds.Tables [ 0 ].Columns.Add ( "DESCRIPTION", typeof ( string ) ); }

                    if (ds.Tables [ 0 ].Columns [ "VALIDABLE" ] == null)
                    { ds.Tables [ 0 ].Columns.Add ( "VALIDABLE", typeof ( bool ) ); }

                else { ;}     //nothing just add the missing columns         
                } //eof else //the ds passed is not null 


            } //eof public static void Method1 


        public static void ChopOffAts ( ref string msg, ref DataSet ds )
            {
            if (ds == null)
                {
                msg = "The dataset passed is null ";
                throw new NullReferenceException ( );
                }
            else
                {
                foreach (System.Data.DataTable dt in ds.Tables) //comm -- we have only one table
                    {
                    foreach (System.Data.DataRow dr in dt.Rows)
                        {   //comm -- null first all the previously filled values 
                        string s = (string)(dr [ "COLUMN_NAME" ]);
                        if (s.StartsWith ( "@" ))
                            {
                            s = s.Substring ( 1, ( s.Length -1) );    //chop off  the first character 
                            //debugOk Utils.Debugger.WriteIf ( "I got after chopping off the first character " + s );
                            dr [ "COLUMN_NAME" ] = (object)s;    //turn back to object
                            }
                        } //eof foreach (System.Data.DataRow dr in dt.Rows)
                    } //foreach (System.Data.DataTable dt in ds.Tables) //comm -- we have only one table

                }
            } //eof public static void ChopOffAts



        public static bool ColumnNameExists ( ref string columnName, ref DataSet pds )
            {
            if (pds == null)
                {
                    Debugger.WriteIf ( "ColumnNameExist passed with null dataset " );
                    
                    return false;
                }
            else
                {
                foreach (System.Data.DataTable pdt in pds.Tables) //comm -- we have only one table
                    {
                    foreach (System.Data.DataRow pdr in pdt.Rows)
                        {   //comm -- null first all the previously filled values 
                        string s = (string)(pdr [ "COLUMN_NAME" ]);
                        if (columnName.Equals(s.Trim()))
                            {
                            return true;
                            } //eof if (columnName .Equals (
                        } //eof foreach (System.Data.DataRow dr in dt.Rows)
                    } //foreach (System.Data.DataTable dt in ds.Tables) //comm -- we have only one table
                return false;
                }

            } //eof public static bool ColumnNameExists ( ref string columnName, ref DataSet ds )


        public static bool setProperty ( ref string msg , ref DataSet pds , string colName ,  object propValue   )
            {
            CheckForUnfilledColumns ( ref msg, ref pds );
            if (pds == null)
                {
                Debugger.WriteIf ( "ColumnNameExist passed with null dataset " );
                msg = "ColumnNameExist passed with null dataset ";
                return false;
                }
            else
                {
                foreach (System.Data.DataTable pdt in pds.Tables) //comm -- we have only one table
                    {
                    foreach (System.Data.DataRow pdr in pdt.Rows)
                        {   //comm -- null first all the previously filled values 
                        pdr [ colName ] = propValue;
                        } //eof foreach (System.Data.DataRow dr in dt.Rows)
                    } //foreach (System.Data.DataTable dt in ds.Tables) //comm -- we have only one table
                
                } //eofelse
            return true;
            } //eof public static bool ColumnNameExists ( ref string columnName, ref DataSet ds )

        public static void Method3 ( ref string msg, ref DataSet ds )
            {
            if (ds == null)
                {
                msg = "The dataset passed is null ";
                throw new NullReferenceException ( );
                }
            else
                {

                }
            } //eof public static void Method1 



public static void GenerateEmptyDataSet ( ref DataSet passedDs)
{

	//PUT HERE YOUR CODE TO GET THE DATASET OBJ / DATATABLE OBJ
	DataSet rds = new DataSet ( );
	DataTable dt = new DataTable ( "MessageTable" );
	// Create a DataColumn instances

	DataColumn dMessage = new DataColumn ( );

	dMessage.ColumnName = "Message";
	dMessage.DataType = Type.GetType ( "System.String" );
	dt.Columns.Add ( dMessage );


		DataRow dr = dt.NewRow ( );
		dr ["Message"] = "No data available";
		dt.Rows.Add ( dr );
		rds.Tables.Add ( dt );
		passedDs = rds;
} //eof method GenerateEmptyDataSet


public static DataSet PopulateMsgDataSet ( ref string msg  )
{

	//PUT HERE YOUR CODE TO GET THE DATASET OBJ / DATATABLE OBJ
	DataSet rds = new DataSet ( );
	DataTable dt = new DataTable ( "0" );
	// Create a DataColumn instances

	
	DataColumn dName = new DataColumn ( );

	dName.ColumnName = "Name";
	dName.DataType = Type.GetType ( "System.String" );


		dt.Columns.Add ( dName );
		DataRow dr = dt.NewRow ( );
		dr ["Name"] = msg;
		// Add the row into the 
		dt.Rows.Add ( dr );
	
		rds.Tables.Add ( dt );
		return rds; 
} //eof method PopulateDataTable
#endregion //eof region Methods






public static void DsToString(ref string echo , ref System.Data.DataSet ds)
{


    if (ds == null)
    {
        echo = "No results found !!!"; 
    } //eof if
    else 
    {
        //foreach (System.Data.DataTable dt in ds.Tables)
        for ( int i = 0 ; i<ds.Tables.Count ;i++ )
        {
            DataTable dt = ds.Tables[i];
            echo = echo + Utils.Data.DataTableGastArbeiter.TableToString(ref dt);
        }	//eof foreach (DataTable dt in ds.Tables)
    } //eof if ds !=null 
} //eof method DsToString



    } //eof class DataSetGastArbeiter

}  //eof namespace Utils